package cn.ncupyh.soft.staticInnerClass;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class dao {
    private static Connection conn = null;
    private static PreparedStatement pstmt = null;
    private static ResultSet rs = null;
    private static String sql = null;

    //先查询    再更新密码
    public static boolean updatePassword(String id,String newP){
        boolean flag = false;
        sql = "select * from djx where id=?";
        try {
            conn = JDBC.getConn();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                System.out.println("存在该学生账号~可进行密码的修改~");
                sql = "update djx set upassword =? where id=?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, newP);
                pstmt.setString(2, id);
                int sum = pstmt.executeUpdate();
                if (sum!=0) {
                    System.out.println("修改成功!");
                    return true;
                }
            }else {
                System.out.println("查无此生!");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBC.release(conn, pstmt, rs);
            pstmt = null;
            conn = null;
            rs = null;
        }
        return flag;
    }

}
